package com.pwc.conversionTool.rest;

import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.getCurrentAccount;

import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.biz.TaskBiz;
import com.pwc.conversionTool.dataAdaptor.DataAdaptor;
import com.pwc.conversionTool.dataAdaptor.IDataAdaptor;
import com.pwc.conversionTool.entity.Formula;
import com.pwc.conversionTool.entity.FormulaRestEntity;
import com.pwc.conversionTool.entity.Task;
import com.pwc.conversionTool.entity.TaskFlow;

@Controller
public class TaskRest {
	private static final Logger logger = LoggerFactory.getLogger(TaskRest.class);

	@Autowired
	public ServletContext context;

	private TaskBiz taskBiz = new TaskBiz();

	@RequestMapping(value = "/rest/task/deleteTaskById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean deleteTaskById(@RequestBody Long id, HttpSession session) {

		try {

			taskBiz.deleteTaskById(id);

			return true;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return false;
	}

	@RequestMapping(value = "/rest/task/getTasksByTaskFlowId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<Task> getTasksByTaskFlowId(@RequestBody Long id) {

		try {

			List<Task> tasks = taskBiz.getTasksByTaskFlowId(id);

			return tasks;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return null;
	}

	@RequestMapping(value = "/rest/task/startTaskFlow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean startTaskFlow(@RequestBody Long id, HttpSession session) {

		try {

			return taskBiz.startTaskFlow(id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return false;
	}

	@RequestMapping(value = "/rest/task/getTaskflows", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<TaskFlow> getTaskflows(HttpSession session) {

		try {
			List<TaskFlow> taskFlows = taskBiz.getTopTaskFlow(getCurrentAccount(session));

			return taskFlows;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return null;
	}

	@RequestMapping(value = "/rest/task/getTaskDataAdaptor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<IDataAdaptor> getTaskDataAdaptor(@RequestBody  FormulaRestEntity formula) {

		try {

			List<IDataAdaptor> dataAdaptors = taskBiz.getDataAdaptor(formula);
			return dataAdaptors;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}	

		return null;
	}

}
